package com.project.service.impl;

import com.project.bean.FacilityBean;
import com.project.mapper.IFacilityMapper;
import com.project.mapper.IRoomMapper;
import com.project.service.IFacilityService;
import org.apache.ibatis.session.SqlSession;
import java.util.List;

public class FacilityServiceImpl extends BaseService implements IFacilityService {

    @Override
    public void add(FacilityBean facility) {
        SqlSession session = this.getSession();
        IFacilityMapper mapper = session.getMapper(IFacilityMapper.class);
        IRoomMapper mapper1 = session.getMapper(IRoomMapper.class);
        mapper.addFacility(facility);
        System.out.println(facility.getId());
        mapper1.updateStatus(facility.getId(),"设施损坏");
        session.commit();
        session.close();
    }

    @Override
    public void handle(int id, int roomId) {
        SqlSession session = this.getSession();
        IFacilityMapper mapper = session.getMapper(IFacilityMapper.class);
        IRoomMapper mapper1 = session.getMapper(IRoomMapper.class);
        mapper.updateFacility(id,"已解决");
        if(mapper.countFacility(roomId, "未解决") == 0){
            mapper1.updateStatus(roomId,"正常");
        }
        session.commit();
        session.close();
    }

    @Override
    public List<FacilityBean> fandById(int roomId) {
        SqlSession session = this.getSession();
        IFacilityMapper mapper = session.getMapper(IFacilityMapper.class);
        List<FacilityBean> list = mapper.findById(roomId);
        session.close();
        return list;
    }

    public static void main(String[] args) {
        IFacilityService service = new FacilityServiceImpl();
//        service.add(new FacilityBean("门坏了",3));
//        service.handle(6,3);
        System.out.println(service.fandById(3));

    }
}
